(function () {
    var module = {};
    var tools = Tools();
    var mobileCode = 0;
    module.init = function () {
        tools.getUserInfo();
        module.toCheck();

        tools.doGet(accountURL+self.apiURL.queryUserInfo,{},module.queryUserInfo);
        //获取城市列表
        tools.doPost(accountURL + tools.apiURL.getCityList, {}, module.getCityList);

        //获取开户支持银行卡
        tools.doGet(accountURL + tools.apiURL.supportBankList, {}, module.supportBankList);

        $(document).on("click", ".openAccount_open", function () {
            $('.goOpenAccount').hide();
        });
        $(document).on("click", ".openAccount_close", function () {
            $('.openAccount_close').attr('href', 'center.html')
        });
        $(document).on("click", ".oa_wcNext", function () {
            $('#member').show();
            $('.oa_wcNext').hide();
        });
      //开户
        $(document).on("click", ".oa_bts", function () {
            mobileCode++;
            if (mobileCode != 1) {
                return;
            }
                if (toCheckBank() && toCheckCard()) {
                    if (toCheckName() && rePassword()) {
                        var datas = {
                            name: $('#User_youName').val(),  //姓名
                            identity: $('#User_Name').val(),  //身份证号码
                            cardNo: $('#User_card').val(),//银行卡号
                            cityId: $("#getCounty").val(),//城市id
                            password: $("#password").val(),//提现密码
                            parentBankId: $("#getBank").val()//总行id
                        };
                        tools.doPost(accountURL + tools.apiURL.openAccount, datas, module.buildOpenAccountParms);
                        $('#accountLoading').show();
                        setTimeout(function () {
                            $('#accountLoading').hide();
                        }, 5000)
                    }
                }

        });
        
        //开户信息
        $(document).on('change', '#getCity', function () {
            //市级
            tools.doPost(accountURL + tools.apiURL.getCityList, {cityId: $(this).val()}, module.getCountyList);
        });
    };
    module.queryUserInfo=function (data) {
        if(data.status==0){
            if(data.data.custodyStatus=='111'||data.data.custodyStatus==111||data.data.custodyStatus=='2'){
                var content=data.data;
                if(content.name!=null){
                    $('#User_youName').val(content.name);
                    $("#User_youName").attr("disabled", true);
                }
                if(content.identity!=null){
                    $('#User_Name').val(content.identity);
                    $("#User_Name").attr("disabled", true);
                }
             }
        }
    };
    module.getCityList = function (data) {
      if(data.status==0){
          //省级
          for (var i = 0; i < data.data.length; i++) {

              $('#getCity').append('<option value="' + data.data[i].id + '">' + data.data[i].name + '</option>')
          }
          //市级
          tools.doPost(accountURL + tools.apiURL.getCityList, {cityId: data.data[0].id}, module.getCountyList);
      }
    };
    module.supportBankList = function (data) {
     if(data.status==0){
         for (var i = 0; i < data.data.length; i++) {
             $('#getBank').append('<option value="' + data.data[i].parentBankId + '">' + data.data[i].name + '</option>')
         }
     }
    };
    module.getCountyList = function (data) {
    if(data.status==0){
        $("#getCounty option").remove();
        for (var i = 0; i < data.data.length; i++) {
            $('#getCounty').append('<option value="' + data.data[i].id + '">' + data.data[i].name + '</option>')
        }
    }
    };
    module.buildOpenAccountParms = function (data) {
        if (data.status == 0) {
            $('#accountLoading').hide();
           window.location.href='openAccountSuccess.html';
        } else {
            $('#accountLoading').hide();
            $("#alertMessage").fadeIn();
            $("#message").text(data.message);
            setTimeout(function () {
                $("#alertMessage").fadeOut();
            }, 2000)
        }
    };
    module.toCheck = function () {
        //身份校验
        $(document).on('keyup', '#User_Name', function () {
            toCheckCard();
        });
        //银行卡
        $(document).on('keyup', '#User_card', function () {
            toCheckBank();
        });

        //用户名
        $(document).on('keyup', '#User_youName', function () {
            toCheckName()
        });

        //重复密码
        $(document).on('keyup', '#password,#rePassword', function () {
            rePassword();
        });
    };
    //重复密码
    function  rePassword() {
        if($('#password').val()==undefined||$('#password').val()==null||$('#password').val()==''){
            $('.correctPd').removeClass('onRight');
            $('.correctPd').addClass('onError');
            return false;
        }else{
            $('.correctPd').removeClass('onError');
            $('.correctPd').addClass('onRight');
        }

        /*重复密码*/
        if($('#rePassword').val() == '' || $('#rePassword').val() == undefined||$('#rePassword').val() == ''){
            $('.correctrePd').removeClass('onRight');
            $('.correctrePd').addClass('onError');
        }else {
            if(!($('#rePassword').val()==$('#password').val())){
                $('.correctrePd').removeClass('onRight');
                $('.correctrePd').addClass('onError');
            }else {

                $('.correctrePd').removeClass('onError');
                $('.correctrePd').addClass('onRight');
            }
        }
        return  true;
    }


    //用户名
    function toCheckName(name) {
        var str = '';
        var tag = true;
        if ($.trim($('#User_youName').val()).length == 0) {
            tag = false;
            mobileCode = 0;
        }
        if (tag) {
            $('.correctName').removeClass('onError');
            $('.correctName').addClass('onRight');
        } else {
            $('.correctName').removeClass('onRight');
            $('.correctName').addClass('onError');
        }
        return tag;
    }

    // 验证身份证
    function isCardNo(card) {
        var pattern = /^([0-9]{17}[0-9X]{1})|([0-9]{15})$/;
        return pattern.test(card);
    }

    function isInd(ind) {
        var pattern = /^\d+(\.\d+)?$/;
        return pattern.test(ind);
    }

    function toCheckCard() {
        var tag = true;
        if ($.trim($('#User_Name').val()).length == 0) {
            tag = false;
            mobileCode = 0;
        } else {
            tag = isCardNo($.trim($('#User_Name').val()));
            mobileCode = 0;
        }
        if (tag) {
            $('.correctIndentity').removeClass('onError');
            $('.correctIndentity').addClass('onRight');
        } else {
            $('.correctIndentity').removeClass('onRight');
            $('.correctIndentity').addClass('onError');
        }
        return tag;
    }

    //校验银行卡
    function toCheckBank() {
        var tag = true;
        if ($.trim($('#User_card').val()).length == 0) {
            tag = false;
            mobileCode = 0;
        } else {
            tag = isInd($.trim($('#User_card').val()));
            /*  if (tools.luhmCheck($.trim($('#User_card').val())) == false) {
             tag = false;
             mobileCode = 0;
             }*/
        }
        if (tag) {
            $('.correctCard').removeClass('onError');
            $('.correctCard').addClass('onRight');
        } else {
            $('.correctCard').removeClass('onRight');
            $('.correctCard').addClass('onError');
        }
        return tag;
    }

    module.init();
})();